package com.arrogames.rgblabyrinth {
	import ash.core.Engine;
	import ash.integration.starling.StarlingFrameTickProvider;
	
	import com.arrogames.lib.ash.animation.AnimationSystem;
	import com.arrogames.lib.ash.movement.MovementSystem;
	import com.arrogames.rgblabyrinth.game.EntityCreator;
	import com.arrogames.rgblabyrinth.game.LevelLoader;
	import com.arrogames.rgblabyrinth.game.camera.CameraRenderingSystem;
	import com.arrogames.rgblabyrinth.game.debug.DebugSystem;
	import com.arrogames.rgblabyrinth.game.rendering.TileRenderingSystem;
	import com.arrogames.rgblabyrinth.game.targeting.TargetingSystem;
	import com.arrogames.rgblabyrinth.game.ui.ExplanationView;
	import com.arrogames.rgblabyrinth.game.ui.GamePanel;
	import com.arrogames.rgblabyrinth.storage.Profile;
	import com.arrogames.rgblabyrinth.storage.RGBStorage;
	
	import starling.core.Starling;
	import starling.display.Sprite;
	import starling.events.Event;
	
	public class RGBLabyrinth extends Sprite {
		
		private var engine : Engine;
		private var tickProvider : StarlingFrameTickProvider;
		
		private var uiContainer:Sprite;
		private var gamePanel:GamePanel;
		
		private var initialised:Boolean;
		private var currentProfile:Profile;

		public function RGBLabyrinth() {
			super();
			initialised = false;
			addEventListener( Event.ADDED_TO_STAGE, init );
		}
		
		public function updateAssetProgress(ratio:Number):void {
			
		}
		
		public function loadingComplete():void {
			startGame();
		}
		
		private function init(event:Event):void {
			var storage:RGBStorage = new RGBStorage();
			if (!storage.hasProfile("Robin")) {
				var profile:Profile = new Profile();
				profile.name = "Robin";
				storage.addProfile(profile);
			}
			
			currentProfile = storage.getProfile("Robin");
			
			initialised = true;
		}
		
		private function startGame():void {
			createUI();
			prepare();
			start();
		}
		
		private function prepare():void {
			engine = new Engine();
			
			var creator:EntityCreator = new EntityCreator(engine);
			var level:LevelLoader = new LevelLoader(creator);
			creator.createGameState(level.map, currentProfile);
			
			engine.addSystem( new MovementSystem(), 15);
			engine.addSystem( new TargetingSystem(gamePanel.gameContent), 16);
			engine.addSystem( new AnimationSystem(), 19);
			//engine.addSystem( new VisionSystem(level.map.tileWidth), 19);
			engine.addSystem( new CameraRenderingSystem(gamePanel.gameContent), 19);
			engine.addSystem( new TileRenderingSystem(gamePanel.gameContent, level.map), 20);
			var debugSystem:DebugSystem = new DebugSystem(gamePanel.gameContent, level.map);
			engine.addSystem(debugSystem , 20);
			//engine.addSystem( new MenuPanningSystem(gamePanel), 20);
			
			trace('ughh, all this epic code.. this cannot have been me :o');
			
			creator.createCamera(0, 0, 800, 600);
			creator.createTiledWorld(level.map);
			level.loadObjectsTmx();
			debugSystem.drawGrid();
		}
		
		private function createUI():void {
			gamePanel = new GamePanel();
			gamePanel.y = 200;
			addChild(gamePanel);
			
			uiContainer = new Sprite();
			uiContainer.addChild(new ExplanationView());
			addChild(uiContainer);
		}
		
		private function start() : void {
			tickProvider = new StarlingFrameTickProvider( Starling.current.juggler );
			tickProvider.add( engine.update );
			tickProvider.start();
		}
	}
}